package routers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"map/controllers"
)

var filterLogin = func(ctx *context.Context) {
	ok := ctx.Input.Session("admin")
	if ok == nil && ctx.Request.RequestURI != "/login" {
		errors := map[string]string{"status": "1", "message": "请登录"}
		js, _ := json.Marshal(errors)
		//ctx.ResponseWriter.Set("Content-Type", "application/json")
		ctx.ResponseWriter.Write(js)
	}
}

func init() {
	beego.InsertFilter("/api/*", beego.BeforeExec, filterLogin)

	beego.Router("/", &controllers.MainController{})

	beego.Router("/login", &controllers.LoginController{}, "post:Login")
	beego.Router("/logOut", &controllers.LoginController{}, "post:LogOut")

	beego.Router("/api/index/getPoi", &controllers.IndexController{}, "get:GetPoi")
	beego.Router("/api/index/config", &controllers.IndexController{}, "get:Config")

	beego.Router("/api/admin/list", &controllers.AdminController{}, "get:List")
	beego.Router("/api/admin/create", &controllers.AdminController{}, "post:Create")
	beego.Router("/api/admin/present", &controllers.AdminController{}, "post:Present")
	beego.Router("/api/admin/delete", &controllers.AdminController{}, "post:Delete")
	beego.Router("/api/admin/update", &controllers.AdminController{}, "post:Update")

	beego.Router("/api/config/info", &controllers.ConfigController{}, "post:Info")
	beego.Router("/api/config/update", &controllers.ConfigController{}, "post:Update")

	beego.Router("/api/cases/create", &controllers.CasesController{}, "post:Create")
	beego.Router("/api/cases/list", &controllers.CasesController{}, "get:List")
	beego.Router("/api/cases/delete", &controllers.CasesController{}, "post:Delete")

	beego.Router("/api/company/create", &controllers.CompanyController{}, "post:Create")

	beego.Router("/api/staff/create", &controllers.StaffController{}, "post:Create")
}
